package com.marc.eden;

import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class User {
    private String account;
    private String password;
    private String lastestLogin;
    private String nickname;

    public User() {
        setAccount(null);
        setPassword(null);
        setLastestLogin(null);
    }

    public User(String account, String password) {
        setAccount(account);
        setPassword(password);
        setLastestLogin(null);
    }

    public void setAccount(String account) {
        if (account == null) {
            this.account = null;
        } else {
            this.account = encodeMD5(account);
        }
    }

    public void setPassword(String password) {
        if (password == null) {
            this.password = null;
        } else {
            this.password = encodeMD5(password);
        }
    }

    public void setLastestLogin(String lastestLogin) {
        if (lastestLogin == null) {
            this.lastestLogin = null;
        } else {
            this.lastestLogin = lastestLogin;
        }
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getAccount() {
        return account;
    }

    public String getPassword() {
        return password;
    }

    public String getLastestLogin() {
        return lastestLogin;
    }

    public String getNickname() {
        return nickname;
    }

    public String register() {
        String result;
        try {
//            URL url = new URL("http://192.168.1.101:8080/EdenServer/communicate");
//            URL url = new URL("http://"+Utils.SERVER_ADDRESS+":3306/EdenServer/eden");
//            URL url = new URL("http://10.18.8.147:8080/EdenServer/eden");
            URL url = new URL("http://140.143.35.110:8080/EdenServer/eden");
            String request = "func=register"+
                    "&account="+getAccount()+
                    "&password="+getPassword()+
                    "&nickname="+getNickname();
            HttpCommunication httpCommunication = new HttpCommunication(url,request);
            httpCommunication.sendMessage();
            System.out.println(httpCommunication.getResponse());

            result = httpCommunication.getResponse();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            result = "error";
        }
        return result;
    }

    public String login() {
        String result;
        try {
//            URL url = new URL("http://192.168.1.102:8080/EdenServer/eden");
//            URL url = new URL("http://"+Utils.SERVER_ADDRESS+":3306/EdenServer/eden");
//            URL url = new URL("http://10.18.8.147:8080/EdenServer/eden");
            URL url = new URL("http://140.143.35.110:8080/EdenServer/eden");
            String request = "func=login"+
                    "&account="+getAccount()+
                    "&password="+getPassword();
            HttpCommunication httpCommunication = new HttpCommunication(url,request);
            httpCommunication.sendMessage();
            System.out.println(httpCommunication.getResponse());
            result = httpCommunication.getResponse();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            result = "error";
        }
        return result;
    }

    public String encodeMD5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
